package com.company.cloud.admin.modules.admin.controller;

import java.util.List;

import com.company.cloud.admin.constant.PlatformAdminConstant;
import com.company.cloud.admin.modules.admin.controller.req.PermissionSaveParam;
import com.company.cloud.admin.modules.admin.controller.req.PermissionUpdateParam;
import com.company.cloud.admin.modules.admin.service.PermissionMgrService;
import com.company.cloud.admin.modules.base.service.PermissionInfoService;
import com.company.cloud.admin.modules.cache.models.PermissionModel;
import com.company.cloud.admin.modules.cache.utils.PTUserConvertUtils;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.tree.CloudTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.admin.modules.base.entity.PermissionInfo;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseParam;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.StrSplitUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Authc
@RestController
@RequestMapping("/admin/permission")
@Api(tags = "UserMgr_权限管理")
public class PermissionMgrController {

    @Autowired
    private PermissionInfoService permissionInfoService;
    @Autowired
    private PermissionMgrService permissionMgrService;


    @PostMapping("/queryTree")
    @ApiOperation(value = "权限树形列表", response = PermissionModel.class)
    public BaseResult tree(BaseParam params) {
        List<PermissionInfo> permissionList = permissionInfoService.list();
        CloudTree<PermissionInfo, PermissionModel> tree = new CloudTree<PermissionInfo, PermissionModel>() {
            @Override
            public boolean isTop(PermissionModel entity) {
                return PlatformAdminConstant.PERMISSION_TOP_CODE.equals(entity.getParentCode());
            }

            @Override
            public boolean isParent(PermissionModel parentObj, PermissionModel childObj) {
                return parentObj.getCode().equals(childObj.getParentCode());
            }

            @Override
            public PermissionModel convert(PermissionInfo entity) {
                return PTUserConvertUtils.getPermissionModel(entity);
            }
        };
        return BaseResult.ok(tree.tree(permissionList));
    }

    @GetMapping("/get")
    @ApiOperation(value = "权限详情信息", response = PermissionModel.class)
    public BaseResult get(@RequestParam("code") String code) {
        PermissionInfo entity = permissionInfoService.getById(code);
        if (entity == null) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        PermissionModel data = PTUserConvertUtils.getPermissionModel(entity);
        return BaseResult.ok(data);
    }

    @PostMapping("/save")
    @ApiOperation(value = "权限新增", response = PermissionModel.class)
    @StaffLogc(title = "权限新增", action = "权限新增", actionAdo = "权限管理")
    public BaseResult save(@RequestBody PermissionSaveParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        PermissionInfo entity = new PermissionInfo();
        entity.setCode(params.getCode());
        entity.setType(params.getType());
        entity.setName(params.getName());
        entity.setShowName(params.getShowName());
        entity.setIcon(params.getIcon());
        entity.setUrl(params.getUrl());
        entity.setState(params.getState());
        entity.setOrders(params.getOrders());
        entity.setShowPlatform(params.getShowPlatform());
        entity.setRemark(params.getRemark());
        entity.setResource(StrSplitUtils.list2string(params.getResource()));
        entity.setParentCode(params.getParentCode());
        entity.addCreateParam(userInfo);
        entity.addEditParam(userInfo);
        permissionInfoService.save(entity);
        PermissionModel data = PTUserConvertUtils.getPermissionModel(entity);
        return BaseResult.ok(data);
    }


    @PostMapping("/update")
    @ApiOperation(value = "权限修改", response = PermissionModel.class)
    @StaffLogc(title = "权限修改", action = "权限修改", actionAdo = "权限管理")
    public BaseResult update(@RequestBody PermissionUpdateParam params) {
        ValidatorUtils.validate(params);
        PermissionInfo entity = permissionInfoService.getById(params.getCode());
        if (entity == null) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        entity.setName(params.getName());
        entity.setShowName(params.getShowName());
        entity.setIcon(params.getIcon());
        entity.setUrl(params.getUrl());
        entity.setState(params.getState());
        entity.setOrders(params.getOrders());
        entity.setShowPlatform(params.getShowPlatform());
        entity.setRemark(params.getRemark());
        entity.setResource(StrSplitUtils.list2string(params.getResource()));
        entity.addEditParam(userInfo);
        permissionInfoService.updateById(entity);
        PermissionModel data = PTUserConvertUtils.getPermissionModel(entity);
        return BaseResult.ok(data);
    }

    @GetMapping("/delete")
    @ApiOperation(value = "权限删除", response = BaseResult.class)
    @StaffLogc(title = "权限删除", action = "权限删除", actionAdo = "权限管理")
    public BaseResult delete(@RequestParam("code") String code) {
        permissionMgrService.delete(code, AuthContextUtils.getUser());
        return BaseResult.ok();
    }


}
